<html>
<head>
	<script type="text/javascript">
		var aJaxURL			= "server-side/operations/document_correction.action.php";		//server side folder url
		var list_aJaxURL	= "server-side/operations/correction/list.action.php";	//server side folder url
		var seoyURL			= "server-side/seoy/seoy.action.php";					//server side folder url
		var tName			= "example";													//table name
		var fName			= "add-edit-form";												//form name
		
		$(document).ready(function () {
            ClearDB();
            
			LoadTable(tName);
						
			/* Add Button ID, Delete Button ID */
			GetButtons("add_button", "");
			
			SetEvents("add_button", "", "", tName, fName, aJaxURL);
		});
		
        //SeoYyy
		$(document.body).click(function (e) {
        	$("#document_number").autocomplete("close");
        });
        
		function LoadTable(table){
			if(table == "prod_list"){
				var menuLength	= [[10], [10]];
				var doc_numb    = $("#document_number").val(); 


					/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
					GetDataTable("prod_list", list_aJaxURL, "get_list", 7, "doc_numb=" + doc_numb, 0, menuLength, 0, "asc");
					GetDataTable("before_act", list_aJaxURL, "get_list_after", 7, "doc_numb=" + doc_numb, 0, menuLength, 0, "asc");
					GetDataTable("after_act", list_aJaxURL, "get_list_diff", 7, "doc_numb=" + doc_numb, 0, menuLength, 0, "asc");
					if(doc_numb != ''){
						GetActInfo(doc_numb);
					}
			}else{
				/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
                GetDataTable(tName, aJaxURL, "get_list", 8, "", 0, "", 0, "desc");
			}
		}
		
		function LoadDialog(form){
			if(form == fName){
				ClearDB();
				
				/* Dialog Form Selector Name, Buttons Array */
				GetDialog(fName, 900, "auto", "");
				
				GetButtons("", "delete_button_prod");
				LoadTable("prod_list");
				SetEvents("", "delete_button_prod", "check-all-prod", "prod_list", "add-edit-prod-form", list_aJaxURL);
				SeoY("document_number", seoyURL, "document_number_name", "", 0);	
			}else{
				SeoY("goods_name", seoyURL, "overhead_goods_name", "", 0);
				SeoY("goods_local_name", seoyURL, "overhead_goods_local_name", "", 0);	
				var buttons = {
			        "save": {
			            text: "შეცვლა",
			            id: "add-production",
			            click: function () {
			            }
			        }
			    };
				GetDialog("add-edit-prod-form", 500, "auto", buttons);
			}
		}
		
		function CloseDialog(form){
			$("#" + form).dialog("close");
		}
		
	    // Add - Save
	    $(document).on("click", "#save-dialog", function () {
		    	param = new Object();
		    	
	            //Action
		    	param.act		= "save_correction";		    
			    
			    param.id		= $("#local_det_id").val();   
			    
				$.ajax({
				        url: aJaxURL,
					    data: param,
				        success: function(data) {
							if(typeof(data.error) != "undefined")
							{
								if(data.error != "")
								{
									alert(data.error);
								}else
								{
									LoadTable(fName);
					        		CloseDialog(fName);
								}
							}
					    }
				    });

		});
		
	    // Add - Save Production
	    $(document).on("click", "#add-production", function () {
	    	param = new Object();

	    	param.act = "correction_done";
	    	param.id       = $("#goods_id").val();
	    	param.quantity = $("#goods_quantity").val();
	    	param.cost     = $("#goods_unit_price").val();
	    	param.c_id     = $("#local_det_id").val();

	    	
            
			    $.ajax({
			        url: aJaxURL,
				    data: param,
			        success: function(data) {
						if(typeof(data.error) != "undefined")
						{
							if(data.error != "")
							{
								alert(data.error);
							}else
							{
								$("#add-edit-prod-form").dialog("destroy");
							}
						}
				    }
			    });
			
		});
		
        $(document).on("click", ".combobox", function (event) {
	    	var i = $(this).text();
			$("#" + i).autocomplete( "search", "" );
		});
		


		
        function ClearDB() {
            $.ajax({
                url: aJaxURL,
                data: "act=clear_db",
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        }
                    }
                }
            });
        }

        function GetActInfo(act_id) {
            $.ajax({
                url: list_aJaxURL,
                data: "act=act_info&act_id="+act_id,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        }else{
                            $("#act_info").html(data.page);
                        }
                    }
                }
            });
        }

        
        /**
         * Set inputs Goods Name & Barcode
         * @param {int} barcode, {string} name,
         */
        function SetGoods(barcode, name) {

            $.ajax({
                url: list_aJaxURL,
                data: "act=get_goods&bc=" + barcode + "&nm=" + name,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            $("#goods_code").val(data.code);
                            $("#goods_name").val(data.name);
                            $("#goods_measure").val(data.measure);
                            $("#goods_unit_price").val(data.price);
                            $("#goods_quantity").val("");
                            $("#goods_quantity").focus();
                        }
                    }
                }
            });
        }

        $(document).on("keydown", "#goods_name", function(event) {
            if (event.keyCode == $.ui.keyCode.ENTER) {
                SetGoods("", this.value);
                event.preventDefault();					//Clear Dialog Default <b>keypress<b> Event
            }
        });


        $(document).on("keypress", "#goods_local_name", function(e) {
             var name = $("#goods_local_name").val();
             if(e.which == 13){
	             	$.ajax({
	                    url: "server-side/documents/overhead/in.action.php",
	                    data: "act=get_unit&name=" + name,
	                    success: function(data) {
	                        if (typeof(data.error) != "undefined") {
	                            if (data.error != "") {
	                                alert(data.error);
	                            }else{
									$("#goods_local_unit").val(data.unit);
									if($("#goods_local_unit").val() != $("#goods_measure option:selected").text()){
										alert("მომწოდებლის ზომის ერთეული არ ემთხვევა თქვენსას!\nმიღებული საქონლის რაოდენობა შეიყვანეთ თქვენი ერთეულის მიხედვით!");
									}
	                        	}
	                        }
	                    }
	                });             
             }
         });
        
    	$(document).on("keypress", "#document_number", function(e) {

    			if(e.which == 13){	
	            	var local_id = GetLocalID();
	            	$("#local_det_id").val(local_id);
	            	
	            	InsertDocDetails(this.value, local_id);
    				var d_n    = $("#document_number").val(); 
    				GetActInfo(d_n);	
    				LoadTable("prod_list");
    			}
    	});

        $(document).on("keyup", "#goods_quantity", function(event) {
            var gq = $("#goods_quantity").val().trim();
            var gup = $("#goods_unit_price").val().trim();

            $("#goods_price").val((gq * gup).toFixed(4));
        });
        //--


        //--Goods In Calculations
        $(document).on("keyup", "#goods_local_quantity", function(event) {
            var gp	= $("#goods_price").val();
            var glq	= $("#goods_local_quantity").val().trim();
            
            $("#goods_local_unit_price").val((gp / glq).toFixed(4));
        });

        $(document).on("keyup", "#goods_unit_price", function(event) {
            var gq = $("#goods_quantity").val().trim();
            var gup = $("#goods_unit_price").val().trim();

            $("#goods_price").val((gq * gup).toFixed(4));

            if (event.keyCode == $.ui.keyCode.ENTER) {
            	$("#add-goods").focus();
                event.preventDefault();					//Clear Dialog Default <b>keypress<b> Event
            }
        });       
        //--

        
        /**
         * Get next auto incremented id
         * @return {int}   Returns a next id
         */
        function GetLocalID() {
            var local_id;
            $.ajax({
                url: aJaxURL,
                async: false, //r-value
                data: "act=get_local_id",
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            local_id = data.local_id;
                        }
                    }
                }
            });
            return local_id;
        }

        function InsertDocDetails(doc_numb, local_id){
            $.ajax({
                url: aJaxURL,
                async: false, //r-value
                data: "act=insert-details&doc_numb="+doc_numb + "&l_id=" + local_id,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            
                        }
                    }
                }
            });
        }


    </script>
</head>

<body>
    <div id="dt_example" class="ex_highlight_row">
        <div id="container" style="width: 1200px;">
            <div id="dynamic">
            	<h2 align="center">დოკუმენტის შესწორება</h2>
            	<div id="button_area">
        			<button id="add_button">დამატება</button>
        		</div>
                <table class="display" id="example">
                    <thead>
                        <tr id="datatable_header">
                            <th>ID</th>
                            <th class="min">რედაქტირების თარიღი</th>
                            <th class="min">მომხმარებელი</th>
                            <th class="min">დოკუმენტის №</th>
                            <th class="min">საწყისი რ–ბა</th>
                            <th class="min">საბოლოო რ–ბა</th>
                            <th class="min">საწყისი თანხა</th>
                            <th class="min">საბოლოო თანხა</th>
                        </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_date" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_start_loc" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_end_loc" value="ფილტრი" class="search_init" />
                            </th>
                             <th>
                                <input type="text" name="search_end_loc" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_end_loc" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_end_loc" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_end_loc" value="ფილტრი" class="search_init" />
                            </th>
                        </tr>
                    </thead>
                </table>
            </div>
            <div class="spacer">
            </div>
        </div>
    </div>
    
    <!-- jQuery Dialog -->
    <div id="add-edit-form" class="form-dialog" title="რედაქტირება">
    	<!-- aJax -->
	</div>
</body>
</html>